﻿using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace HugMe.Hepler
{
	public static class HtmlHeplerExtention
	{
		public static IHtmlString Pages(this HtmlHelper html, int page)
		{
			StringBuilder sb = new StringBuilder();


			int fromPage = page - 2 < 1 ? 1 : page - 2;
			int toPage = fromPage + 4;

			bool isNotLeft = (fromPage <= 1);

			TagBuilder li = new TagBuilder("li");
			if (isNotLeft)
			{
				li.AddCssClass("disabled");
				li.InnerHtml = "<a href=\"#\">&laquo;</a>";
			}
			else
			{
				li.InnerHtml = html.ActionLink(HttpUtility.HtmlDecode("&laquo;"), "Index", "Vk", new { page = fromPage - 1 }, new { }).ToHtmlString();
			}
			sb.Append(li.ToString());

			for (int i = fromPage; i <= toPage; i++)
			{
				li = new TagBuilder("li");
				if (i == page)
				{
					li.AddCssClass("disabled");
					li.InnerHtml = "<a href=\"#\">" + i + "</a>";
				}
				else
				{
					li.InnerHtml = html.ActionLink(i.ToString(CultureInfo.InvariantCulture), "Index", "Vk", new { page = i }, new { }).ToHtmlString();
				}

				sb.Append(li.ToString());
			}

			li = new TagBuilder("li");
			li.InnerHtml = html.ActionLink(HttpUtility.HtmlDecode("&raquo;"), "Index", "Vk", new { page = toPage + 1 }, new { }).ToHtmlString();
			sb.Append(li.ToString());

			return new MvcHtmlString(sb.ToString());
		}
	}
}